from typing import List, Any

from fastapi import WebSocket


class ConnectionManager:
    __slots__ = ['active_connections']

    def __init__(self):
        self.active_connections: List[WebSocket] = []

    async def connect(self, websocket: WebSocket):
        await websocket.accept()
        self.active_connections.append(websocket)

    def disconnect(self, websocket: WebSocket):
        self.active_connections.remove(websocket)

    @staticmethod
    async def send_message(message: Any, websocket: WebSocket):
        await websocket.send_json(data=message)

    async def broadcast(self, message: str):
        for connection in self.active_connections:
            await connection.send_json(data=message)
